//
//  ImageTableViewController.m
//  HeXun
//
//  Created by 6009 on 15/11/16.
//  Copyright © 2015年 OC_2_1. All rights reserved.
//

#import "PhotosTableViewController.h"
#import "PhotosTableViewCell.h"
#import "PhotosViewModel.h"
#import "UIImageView+WebCache.h"
#import "GetXMLObject.h"
#import "NEWSModel.h"
#import "PhotosTableViewCell2.h"
#import "PhotosTableViewCell3.h"
#import "PhotoInfoViewController.h"
#import "MBProgressHUD.h"
@interface PhotosTableViewController ()
{
    NSArray *_arrData;
    NSArray *_stockArr;
    NSArray *_carArr;
    NSArray *_houseArr;
    NSArray *_allArr;
}
@end

@implementation PhotosTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.stockPhotoButton.tag = 1;
    self.carPhotoButton.tag = 2;
    self.housePhotoButton.tag = 3;
    self.allPhotoButton.tag = 4;
    [self.stockPhotoButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.carPhotoButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.housePhotoButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.allPhotoButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action:) name:@"Press" object:nil];
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    NSBlockOperation* cOperation = [NSBlockOperation blockOperationWithBlock:^{
        
        GetXMLObject* xmlObject = [GetXMLObject new];
        //股票图片
        _stockArr = [xmlObject getPhotoNEWSArrayFromBlockID:132138528];
        //汽车图片
        _carArr = [xmlObject getPhotoNEWSArrayFromBlockID:126595734];
        //房产图片
        _houseArr = [xmlObject getPhotoNEWSArrayFromBlockID:126649497];
        //综合图片
        _allArr = [xmlObject getPhotoNEWSArrayFromBlockID:126595066];
        
        _arrData = [NSArray arrayWithArray:_stockArr];
        NSBlockOperation* dOperation = [NSBlockOperation blockOperationWithBlock:^{
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        }];
        //涉及更新UI的操作，就要创建包含这些操作的Operation对象，并把它放入主线程队列
        NSOperationQueue* mainQueue = [NSOperationQueue mainQueue];
        //当Operation放入主线程队列中，其操作就是子主线程中执行
        [mainQueue addOperation:dOperation];
    }];
    
    //创建Operation队列,operation只有在队列里才能跑
    NSOperationQueue* takeMoneyQueue = [NSOperationQueue new];
    
    [takeMoneyQueue addOperation:cOperation];
    
    
    
}

- (void)action:(NSNotification* )notification
{
    UIButton* button = notification.object;
    NSLog(@"%ld",button.tag);
    if (button.tag == 1)
    {
        _arrData = _stockArr;
        
    }
    if (button.tag == 2)
    {
        _arrData = _carArr;
    }
    if (button.tag == 3)
    {
        _arrData = _houseArr;
    }
    if (button.tag == 4)
    {
        _arrData = _allArr;
    }
    [self.tableView reloadData];
}


- (void)buttonPress:(UIButton* )button
{
    //发送消息
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Press" object:button];
    NSLog(@"button.tag= %ld",button.tag);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GetXMLObject* xmlObject = [GetXMLObject new];
    NEWSModel* newsModel = _arrData[indexPath.row];
    NSArray* imgStrArr = [xmlObject getStrArrFrom:newsModel.matchpic using:@"|"];
    if (imgStrArr.count >= 1 && imgStrArr.count <= 8) {
        PhotosTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"img"];
        cell.titleLabel.text = newsModel.title;
        cell.dateLabel.text = @"...";
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:imgStrArr[0]]];
        return cell;
    }else if (imgStrArr.count >= 9 && imgStrArr.count <= 12)
    {
        PhotosTableViewCell2* cell = [tableView dequeueReusableCellWithIdentifier:@"img2"];
        cell.titleLabel.text = newsModel.title;
        cell.dateLabel.text = @"...";
        [cell.imgView1 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[0]]];
        [cell.imgView2 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[1]]];
        [cell.imgView3 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[2]]];
        return cell;
    }else
    {
        PhotosTableViewCell3* cell = [tableView dequeueReusableCellWithIdentifier:@"img3"];
        cell.titleLabel.text = newsModel.title;
        [cell.imgView1 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[0]]];
        [cell.imgView2 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[1]]];
        [cell.imgView3 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[2]]];
        [cell.imgView4 sd_setImageWithURL:[NSURL URLWithString:imgStrArr[3]]];
        return cell;
    }
    return nil;
}


#pragma mark - itemDelegate
-(void)navigationItemToTabarControllerItem
{
    self.tabBarController.navigationItem.leftBarButtonItem = self.navigationItem.leftBarButtonItem;
    self.tabBarController.navigationItem.rightBarButtonItem = self.navigationItem.rightBarButtonItem;
    self.tabBarController.navigationItem.titleView = self.navigationItem.titleView;
    self.tabBarController.navigationItem.title = self.navigationItem.title;
    self.navigationController.navigationBarHidden = NO;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PhotoInfoViewController* photoInfoViewController = segue.destinationViewController;
    NSInteger i = [self.tableView indexPathForSelectedRow].row;
    photoInfoViewController.newsModel = _arrData[i];
}


@end
